﻿using FashionWebsite.Models.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FashionWebsite.Models.DAL
{
    public enum SearchType
    {
        PostedDate,
        Price,
        Status,
        View
    }

    public enum SearchOrder
    {
        ASC,
        DESC
    }
    public class DalProduct
    {
        ShopOnlineDataContext data = new ShopOnlineDataContext();
        public List<Product> product { get; set; }
        public Category category { get; set; }
        public List<Product> getListProducer()
        {
            return (from c in data.Products select c).ToList();
        }
        public static IEnumerable<Product> GetProducts(int? productTypeID, SearchType searchType, SearchOrder searchOrder, int page, int numberItemsInPage = 4)
        {
       
            ShopOnlineDataContext data1 = new ShopOnlineDataContext();
            string mainCondition = productTypeID == null ? "1 = 1" : string.Format("{0} = {1}", FConst.PRODUCT_TYPE, productTypeID);
            string sqlQuery = SearchWithMainCondition(searchType, searchOrder, page, numberItemsInPage, mainCondition);
            return data1.ExecuteQuery<Product>(sqlQuery);
        }
        public static IEnumerable<Product> GetProductBy(int? id )
        {
            ShopOnlineDataContext data1 = new ShopOnlineDataContext();
            if (id == 3)
            {

                return (from product in data1.Products  orderby (product.ProductId) ascending select product );
            }
            if (id == 4)
            {

                return (from product in data1.Products  orderby (product.ProductId) descending select product);
            }
            if (id == null)
            {

                return (from product in data1.Products select product);
            }

            if (id == 1)
            {

                return (from product in data1.Products where product.State == 1 orderby (product.ProductId) descending select product);
            }
            else
                
                    return (from product in data1.Products select product);
               
        }
        public static IEnumerable<Product> GetProductsByCaregory(int? categoryId)
        {
            ShopOnlineDataContext data1 = new ShopOnlineDataContext();
            return (from product in data1.Products where product.CategoryId == categoryId select product);

        }
        public page<Product> GetProductsState()
        {
            
            var persons = from c in data.Products select c;
            var people = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = persons.ToList();
            people.Data = ctx.product.OrderBy(p => p.ProductId).Take(2).ToList();
            people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / 2));
            return people;

        }
        public page<Product> test(int page)
        {
            var persons = from c in data.Products select c;
            var people = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = persons.ToList();
            people.Data = ctx.product.OrderBy(p => p.ProductId).Skip(2 * (page - 1)).Take(2).ToList();
            people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / 2));
            people.CurrentPage = page;


            return people;
        }

        internal static int GetProductsCount(int? categoryId = null)
        {
            ShopOnlineDataContext data1 = new ShopOnlineDataContext();
            if (categoryId == null)
            {
                return (from product in data1.Products  select product.ProductId).Count();
            }
            else
            {
                return (from product in data1.Products where product.CategoryId == categoryId select product.ProductId).Count();
            }
        }
        


       
        private static string SearchWithMainCondition(SearchType searchType, SearchOrder searchOrder, int page, int numberItemsInPage,  string mainCondition)
        {
            int begin = (page - 1) * numberItemsInPage;
            int end = page * numberItemsInPage;
            string sqlQuery = "";
            string searchTypeString = GetSearchTypeString(searchType);

            string searchOrderString = searchOrder == SearchOrder.ASC ? FConst.ASC : FConst.DESC;

            //string isDeletedCondition = isDeleted ? string.Format(IS_DELETED_CONDITION_TEMPLATE, 1) : string.Format(IS_DELETED_CONDITION_TEMPLATE, 0);


            sqlQuery = String.Format("SELECT * FROM (select Row_Number() over (order by ProductID) as RowIndex, *  FROM {0} WHERE {1}) as Sub   Where Sub.RowIndex > {2} and Sub.RowIndex  <={3}  ORDER BY {4} {5}",
                   FConst.PRODUCT_TABLE_NAME, mainCondition, begin, end, searchTypeString, searchOrderString);

            return sqlQuery;

        }
       
        public static string GetSearchTypeString(SearchType searchType)
        {
            return //searchType == SearchType.PostedDate ? FConst.POSTED_DATE :
                searchType == SearchType.Price ? FConst.PRODUCT_PRICE : searchType == SearchType.View ? FConst.PRODUCT_VIEW : FConst.PRODUCT_STATUS;
        }
    }
}